﻿#region
/*******************************************************************************
** File Name:		    RightsTypeService.cs
** Creator:		        chao.jiang 
** EMail:               chao.jiang 
** Create date:	        
** Latest Modifier:
** Latest Modify date:
** Description:		    
** Modify History:
**
** Code Review:
**
** Version number:       1.0.0
*********************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.IBatisRepository;
using Phoenix.Respository;
using System.Data;
using Phoenix.Entity;
using Phoenix.Cache;

namespace Phoenix.Service
{
    public class RightsTypeService : IRightsTypeService
    {
        private const string prefix = "Phoenix:RightsAndType";
        ICacheProvider _cacheProvider = CacheProviderFactory.HashTable().CreateCacheProvider();

		 IRightsType rightsType = new RightsTypeRepository();
         IRightsService IRights = new RightsService();
        		 
		 #region  Auto Generation

         public void AddRightsType(RightsTypeModel model)
        {
			ERightsType entity = ConvertModelToEntity(model);
			
             rightsType.AddRightsType(entity);
        }

		public void UpdateByTypeID (RightsTypeModel model)
		{
			ERightsType entity = ConvertModelToEntity(model);

            rightsType.UpdateByTypeID(entity);

		}

		public void DeleteByTypeID (System.Int32 typeID)
		{
		
            rightsType.DeleteByTypeID(typeID);

		}
		 
        public RightsTypeModel GetByTypeID (System.Int32 typeID)
        {
        
			ERightsType entity = rightsType.GetByTypeID(typeID);
			
            RightsTypeModel model = ConvertEntityToModel(entity);


            return model;
        }
        
		public RightsTypeModel ConvertEntityToModel(ERightsType entity)
		{
			if (entity == null)
            {
                return null;
            }
			RightsTypeModel model = new RightsTypeModel();
                        model.TypeID = entity.TypeID;
                        model.TypeName = entity.TypeName;
                        model.ParentTypeID = entity.ParentTypeID;
                        model.SystemFlag = entity.SystemFlag;
                        model.CreateTime = entity.CreateTime;
                        model.CreateUser = entity.CreateUser;
                        model.UpdateTime = entity.UpdateTime;
                        model.UpdateUser = entity.UpdateUser;
                        return model;
		}
		
		public ERightsType ConvertModelToEntity(RightsTypeModel model)
		{
			if (model == null)
            {
                return null;
            }
			ERightsType entity = new ERightsType();
                        entity.TypeID = model.TypeID;
                        entity.TypeName = model.TypeName;
                        entity.ParentTypeID = model.ParentTypeID;
                        entity.SystemFlag = model.SystemFlag;
                        entity.CreateTime = model.CreateTime;
                        entity.CreateUser = model.CreateUser;
                        entity.UpdateTime = model.UpdateTime;
                        entity.UpdateUser = model.UpdateUser;
                        return entity;
		}
		
		#endregion

        public IList<RightsTypeModel> GetAll()
        {
            IList<ERightsType> entityList = rightsType.GetAll();
            if (entityList == null)
                return null;

            IList<RightsTypeModel> modelList = new List<RightsTypeModel>();

            foreach (var item in entityList)
            {
                RightsTypeModel model = ConvertEntityToModel(item);
                modelList.Add(model);
            }
            return modelList;
        }

        /// <summary>
        /// 获取所有的权限和类别 from cache
        /// </summary>
        /// <returns></returns>
        public IList<RightsAndTypeModel> GetRightsAndType()
        {
            if (_cacheProvider.Contains(prefix))
            {
                return _cacheProvider.Get(prefix) as IList<RightsAndTypeModel>;
            }
            else
            {
                IList<RightsAndTypeModel> objList = GetRightsAndTypeFromDB();
                if (objList != null)
                    _cacheProvider.Add(prefix, objList);

                return objList;
            }
        }

        //移除权限和类别缓存
        public void RemoveRightsAndTypeCache()
        {
            if (_cacheProvider.Contains(prefix))
            {
                _cacheProvider.Remove(prefix);
            }
        }


        private IList<RightsExtModel> ConverToExtModel(IList<RightsModel> list)
        {
            IList<RightsExtModel> extList = new List<RightsExtModel>();

            foreach(var item in list)
            {
                RightsExtModel model =new RightsExtModel()
                {
                    Checked=false,
                    Enable=true,
                    RightDisplayName = item.RightDisplayName,
                    RightID = item.RightID,
                    RightMemo = item.RightMemo,
                    RightName = item.RightName,
                    RightTypeID = item.RightTypeID
                };
                extList.Add(model);
            }

            return extList;
        }

        private IList<RightsAndTypeModel> GetRightsAndTypeFromDB()
        {
            IList<RightsTypeModel> rightsTypeList = GetAll();

            IList<RightsAndTypeModel> rightsAndTypList = new List<RightsAndTypeModel>();

            foreach (var item in rightsTypeList)
            {
                RightsAndTypeModel model = new RightsAndTypeModel();

                IList<RightsModel> rightsModelList = IRights.GetByRightsTypeID(item.TypeID);

                model.Rights = ConverToExtModel(rightsModelList);

                model.RightsType = item;

                rightsAndTypList.Add(model);
            }
            return rightsAndTypList;
        }
 

        public void DeleteRightAndType(int typeID)
        {
            rightsType.DeleteRightAndType(typeID);
        }
    }
}